package org.wsiz.vro.web.converter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.wsiz.vro.domain.Account;
import org.wsiz.vro.domain.Address;
import org.wsiz.vro.domain.Role;
import org.wsiz.vro.domain.Film;
import org.wsiz.vro.repository.AccountRepository;
import org.wsiz.vro.repository.AddressRepository;
import org.wsiz.vro.repository.RoleRepository;
import org.wsiz.vro.repository.FilmRepository;

/**
 * Responsible for creating Entity JSF converters. Each converter provides a
 * 'print' method to convert an entity instance to a friendly string
 * representation (readable by humans...).
 */
@Configuration
public class EntityConverterConfig {

	@Autowired
	AccountRepository accountRepository;

	@Bean
	public AccountConverter accountConverter() {
		return new AccountConverter(accountRepository);
	}

	public class AccountConverter extends GenericConverter<Account, Integer> {
		public AccountConverter(AccountRepository accountRepository) {
			super(accountRepository, "email");
		}
	}

	@Autowired
	AddressRepository addressRepository;

	@Bean
	public AddressConverter addressConverter() {
		return new AddressConverter(addressRepository);
	}

	public class AddressConverter extends GenericConverter<Address, Integer> {
		public AddressConverter(AddressRepository addressRepository) {
			super(addressRepository, "streetName + '/' + city");
		}
	}

	@Autowired
	FilmRepository filmRepository;

	@Bean
	public FilmConverter filmConverter() {
		return new FilmConverter(filmRepository);
	}

	public class FilmConverter extends GenericConverter<Film, Integer> {
		public FilmConverter(FilmRepository filmRepository) {
			super(filmRepository, "title");
		}
	}

	@Autowired
	RoleRepository roleRepository;

	@Bean
	public RoleConverter roleConverter() {
		return new RoleConverter(roleRepository);
	}

	public class RoleConverter extends GenericConverter<Role, Integer> {
		public RoleConverter(RoleRepository roleRepository) {
			super(roleRepository, "roleName");
		}
	}
}